package servlet.tea;

import com.alibaba.fastjson.JSONObject;
import entity.Homework;
import util.JdbcUtil;
import util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/modifywork")
public class ModifyWorkServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject data = JsonUtil.getJson(req);  // receive JSON-data
        String id = data.getString("id");
        String content = data.getString("content");
        String requirement = data.getString("requirement");
        String deadline = data.getString("deadline");

        String sql = "select * from homework where id = ?";
        Homework homework = JdbcUtil.queryOne(Homework.class, sql, id);
        JdbcUtil.exeUpdate("delete from homework where id = ?", id);
        JdbcUtil.exeUpdate("insert into homework value(?, ?, ?, ?, ?)", id, content, requirement, deadline, homework.getTeacher_id());

        JSONObject respJson = new JSONObject();  // return JSON-data
        respJson.put("code", 200);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().write(String.valueOf(respJson));
    }
}
